package Service.Impl;


import Dao.JZFWTJDao;
import Model.Department;
import Model.FWJGHZ;
import Model.JZFWTJ;
import Service.JZFWTJService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import utils.Excel;

import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("jzfwtjService")
public class JZFWTJServiceImpl implements JZFWTJService {

    @Autowired
    private JZFWTJDao jzfwtjDao;


    //得到可控范围表
    @Override
    public List<JZFWTJ> getAllList() {
        return this.jzfwtjDao.getAllList();
    }

    //查询
    @Override
    public  List<JZFWTJ> findBySection(String kkfw_section){
        System.out.println("进入JJFWTJservice层");
        return jzfwtjDao.findBySection(kkfw_section);
    }

    // 删除
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 30000, rollbackFor = {RuntimeException.class, Exception.class})
    public String delKkfwb(int id) {
        System.out.println("service层：执行删除");
        int num = this.jzfwtjDao.deleteById(id);
        if (num < 1) {//事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作失败";
        }
        return "ok";
    }

    //修改
    @Override
    public String updateKkfwb(JZFWTJ jzfwtj, HttpSession session) {
        System.out.println("1111进入service"+session);
        System.out.println("3333"+jzfwtj.getId());
        if (jzfwtj.getId() != null) {
            System.out.println("2222:"+jzfwtj.getId());
            this.jzfwtjDao.updateContent(jzfwtj);
            System.out.println("update成功（Impl）");
        }
        return "ok";
    }

   // excel导入
    @Override
    public String uploadFile(InputStream is, String originalFilename) {
        System.out.println("进入service" );
        Map<String,Object> ginsengMap = new HashMap<String,Object>();//建立一个新的哈希表
        List<ArrayList<Object>> list;//新建一个列表
        if (originalFilename.endsWith(".xls")) {
            System.out.println("读到的文件2003" );
            list = Excel.readExcel2003(is);
        } else {
            System.out.println("读到的文件2007" );
            list = Excel.readExcel2007(is);
            System.out.println("list"+list);
        }

        for (int i=0,j=list.size();i<j-1;i++){
            System.out.println("service，获取i："+i);
            List<Object> row = list.get(i);
            System.out.println("service，获取row："+row);
            ginsengMap.put("id", row.get(0).toString());
            ginsengMap.put("kkfw_ID", row.get(1).toString());
            ginsengMap.put("kkfw_section", row.get(2).toString());
            ginsengMap.put("kkfw_area", row.get(3).toString());
            ginsengMap.put("kkfw_apartment", row.get(4).toString());

            Object section=jzfwtjDao.selectBySection(row.get(2).toString());
            System.out.println(section);
            if(section==null) {
                jzfwtjDao.uploadFile(ginsengMap);
                System.out.println(" 插入 "+ginsengMap);
            }else{
                jzfwtjDao.updateFileBySection(ginsengMap);
                System.out.println(" 更新 "+ginsengMap);
            }
        }

        System.out.println("service：获取完毕" );

        return "01";
    }

   // 导出可控范围表
    @Override
    public List<JZFWTJ> OutputExcel() {
        return jzfwtjDao.getAllList();
    }





   //街镇服务统计列表并查询
    @Override
    public  List<Map<String,Object>> getFwjgList(String department_name){
        return this.jzfwtjDao.getFwjgList(department_name);
    }


//    @Override
//    public  String deleteByFwjgId(int id){
//        System.out.println("service层：执行删除");
//        int num = this.jzfwtjDao.deleteByFwjgId(id);
//        if (num < 1) {//事务回滚
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//            return "操作失败";
//        }
//        return "ok";
//    }




}
